var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = e(require("../../@babel/runtime/regenerator")), n = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), a = require("../../@babel/runtime/helpers/asyncToGenerator"), r = e(require("../../behavior/payBehavior")), o = e(require("../../http/coupon")), s = e(require("../../http/order")), i = e(require("../../http/address")), c = require("../../utils/tool"), u = getApp(), d = require("../../utils/api.js"), p = new o.default(), l = new s.default();

new i.default();

Component({
    behaviors: [ r.default ],
    prepay_id: "",
    options: {
        addGlobalClass: !0
    },
    properties: {
        image: String,
        title: String,
        itemNo: String,
        seriesId: String,
        productId: String,
        boxId: String,
        boxType: String,
        priceInfo: Object,
        couponSelected: {
            type: Object,
            observer: function(e, t) {
                console.log("【优惠券】切换", t, "=>", e), this.updateRealAmount();
            }
        },
        brightBoxDetails: {
            type: Object
        }
    },
    data: {
        canUseCoupon: [],
        realAmount: 0,
        showing: !0
    },
    lifetimes: {
        attached: function() {
            console.log("attached", this.data.priceInfo), this.updateRealAmount(), this.loadCouponList();
        },
        detached: function() {
            this.data.showing = !1;
        }
    },
    detached: function() {
        this.data.showing = !1;
    },
    methods: {
        nullOnTap: function() {},
        updateRealAmount: function() {
            var e = this.data.priceInfo.Price;
            this.data.couponSelected && (1 == this.data.couponSelected.sceneType ? e -= this.data.couponSelected.amount : 2 == this.data.couponSelected.sceneType || 3 == this.data.couponSelected.sceneType && (e = this.data.couponSelected.changePrice)), 
            e < 0 && (e = 0), this.setData({
                realAmount: parseFloat(e.toFixed(2))
            });
        },
        loadCouponList: function() {
            var e = this;
            return a(t.default.mark(function a() {
                var r, o, s, i, c, u;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return r = e.data.productId, o = {
                            productId: r,
                            quantity: 1,
                            integrationSwitch: 0
                        }, t.next = 4, p.getCouponByGoods(o);

                      case 4:
                        s = t.sent, i = s.data.canUseCoupon, c = n(i);
                        try {
                            for (c.s(); !(u = c.n()).done; ) u.value.select = !1;
                        } catch (e) {
                            c.e(e);
                        } finally {
                            c.f();
                        }
                        e.setData({
                            canUseCoupon: i,
                            noCanUseList: s.data.noCanUseList
                        });

                      case 9:
                      case "end":
                        return t.stop();
                    }
                }, a);
            }))();
        },
        couponOnTap: function() {
            this.triggerEvent("showCouponListOnTap", {
                canUseCoupon: this.data.canUseCoupon,
                noCanUseList: this.data.noCanUseList
            });
        },
        payOnTap: function() {
            this.pay();
        },
        pay: function() {
            var e = this;
            return a(t.default.mark(function n() {
                var a, r, o, s, i, d, p, h, f;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return u.showLoading(), t.prev = 1, a = {
                            boxId: e.data.boxId,
                            quantity: 1,
                            couponHistoryId: e.data.couponSelected ? e.data.couponSelected.couponHistoryId : null,
                            integrationSwitch: 0
                        }, t.next = 5, e._getDefaultAddressId();

                      case 5:
                        if (r = t.sent) {
                            t.next = 9;
                            break;
                        }
                        return e.triggerEvent("showSendAgreement", {}), t.abrupt("return");

                      case 9:
                        return a.addressId = r, t.next = 12, l.createBrightOrder(a);

                      case 12:
                        return o = t.sent, s = o.data.orderId, e.orderId = s, console.log("【开盒】订单号", s), 
                        t.next = 18, (0, c.promising)(wx.login)();

                      case 18:
                        return i = t.sent, d = i.code, t.next = 22, l.getWxPayInfo(d, s);

                      case 22:
                        p = t.sent, h = JSON.parse(p.data), f = {
                            timeStamp: h.timeStamp,
                            nonceStr: h.nonceStr,
                            package: h.package,
                            paySign: h.paySign,
                            signType: h.signType,
                            success: function(t) {
                                e.paySuccess();
                            },
                            fail: function(t) {
                                e.cancelPay();
                            }
                        }, wx.requestPayment(f), t.next = 32;
                        break;

                      case 28:
                        t.prev = 28, t.t0 = t.catch(1), console.log(t.t0), u.showAlert("提示", "付款失败");

                      case 32:
                        return t.prev = 32, wx.hideLoading(), t.finish(32);

                      case 35:
                      case "end":
                        return t.stop();
                    }
                }, n, null, [ [ 1, 28, 32, 35 ] ]);
            }))();
        },
        paySuccess: function() {
            var e = this;
            return a(t.default.mark(function n() {
                var a;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return u.showLoading("开盒中…"), t.prev = 1, t.next = 4, d.delay(new Date().getTime(), 5e3, {});

                      case 4:
                        return t.sent, t.next = 7, l.getOpenResult(e.orderId);

                      case 7:
                        a = t.sent, e.triggerEvent("onOpenSuccess", a.data), t.next = 14;
                        break;

                      case 11:
                        t.prev = 11, t.t0 = t.catch(1), console.log(t.t0);

                      case 14:
                        return t.prev = 14, wx.hideLoading(), t.finish(14);

                      case 17:
                      case "end":
                        return t.stop();
                    }
                }, n, null, [ [ 1, 11, 14, 17 ] ]);
            }))();
        },
        cancelPay: function() {
            var e = this;
            return a(t.default.mark(function n() {
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.prev = 0, t.next = 3, l.cancelOrder(e.orderId);

                      case 3:
                        t.sent, t.next = 9;
                        break;

                      case 6:
                        t.prev = 6, t.t0 = t.catch(0), console.log(t.t0);

                      case 9:
                      case "end":
                        return t.stop();
                    }
                }, n, null, [ [ 0, 6 ] ]);
            }))();
        }
    }
});